package UI;

import Logic.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import Util.*;

public class LoginJFrame extends JFrame implements MouseListener {
    static ArrayList<User> list = new ArrayList<>();

    static {
        list.add(new User("zhangsan", "123456"));
        list.add(new User("lisi", "123456"));
    }


    JTextField usernameText = new JTextField();
    JTextField passwordText = new JTextField();
    JTextField VerifyText = new JTextField();
    JLabel code = new JLabel();

    JButton login = new JButton();
    JButton register = new JButton();

    String codeNum;

    public LoginJFrame() {
        InitJFrame();
        InitImage();

        this.setVisible(true);

    }

    private void InitImage() {
        //用户名
        Font usernameFont = new Font(null, 1, 16);
        JLabel user = new JLabel("用户名");
        user.setForeground(Color.BLUE);

        user.setFont(usernameFont);

        user.setBounds(140, 55, 55, 22);

        this.getContentPane().add(user);

        //输入框
        usernameText.setBounds(223, 46, 200, 30);
        this.getContentPane().add(usernameText);


        //密码
        Font PassWordFont = new Font(null, 1, 16);
        JLabel password = new JLabel("密码");
        password.setForeground(Color.BLUE);
        password.setFont(PassWordFont);
        password.setBounds(197, 95, 40, 22);

        this.getContentPane().add(password);

        //输入框
        passwordText.setBounds(263, 87, 160, 30);
        this.getContentPane().add(passwordText);

        //验证码
        Font VerifyFont = new Font(null, 1, 16);
        JLabel verify = new JLabel("验证码");
        verify.setForeground(Color.BLUE);
        verify.setFont(VerifyFont);
        verify.setBounds(215, 142, 55, 22);
        this.getContentPane().add(verify);

        //输入框
        VerifyText.setBounds(291, 133, 100, 30);
        this.getContentPane().add(VerifyText);


        //验证码提示
        Font codeFont = new Font(null, 2, 14);
        codeNum = codeUtils.getCode();

        code.addMouseListener(this);
        code.setForeground(Color.GREEN);
        code.setFont(codeFont);
        code.setText(codeNum);
        code.setBounds(400, 133, 100, 30);
        this.getContentPane().add(code);

        //设置登录按钮
        login.setBounds(123, 310, 128, 47);

        login.setIcon(new ImageIcon("src\\Image\\Login\\微信图片_20241124120028.jpg"));

        //去除边框
        login.setBorderPainted(false);
        //去除背景
        login.setContentAreaFilled(false);

        login.addMouseListener(this);
        this.getContentPane().add(login);

        register.setBounds(256,310,128,47);
        register.setIcon(new ImageIcon("src\\Image\\Login\\微信图片_20241124120027.jpg"));

        register.setBorderPainted(false);
        register.setContentAreaFilled(false);

        register.addMouseListener(this);
        this.getContentPane().add(register);


        //背景图片
        JLabel background=new JLabel(new ImageIcon("src\\Image\\Login\\微信图片_20241124120030.jpg"));
        background.setBounds(0,0,633,423);
        this.getContentPane().add(background);
    }

    private void InitJFrame() {

//        this.addMouseListener(this);
        this.setSize(633, 423);
        this.setTitle("斗地主单击版1.0登录");

        this.setLocationRelativeTo(null);//居中

        this.setDefaultCloseOperation(3);//关闭模式

        this.setAlwaysOnTop(true);//置顶

        this.setLayout(null);//取消内部默认布局

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object oj=e.getSource();
        if(oj==code){
            System.out.println("点击");
            codeNum=codeUtils.getCode();
            code.setText(codeNum);
            this.getContentPane().repaint();
        }

    }

    @Override
    public void mousePressed(MouseEvent e) {
        Object oj=e.getSource();
        if(oj==login){
            login.setIcon(new ImageIcon("src\\Image\\Login\\微信图片_20241124120029.jpg"));
            this.getContentPane().repaint();
        }
        if(oj==register){
            register.setIcon(new ImageIcon("src\\Image\\Login\\微信图片_20241124120026.jpg"));
            this.getContentPane().repaint();
        }

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        Object oj=e.getSource();
        if(oj==login){
            login.setIcon(new ImageIcon("src\\Image\\Login\\微信图片_20241124120028.jpg"));
            this.getContentPane().repaint();
        if (VerifyText.getText().isEmpty()) {
            ShowJDialog("验证码不能为空!");
        } else if (usernameText.getText().isEmpty() || passwordText.getText().isEmpty()) {
            ShowJDialog("用户名或密码不能为空！");
        } else {
            if (VerifyJudge(VerifyText.getText())) {
                if (UsernameJudge(usernameText.getText())) {
                    if (PasswordJudge(passwordText.getText())) {
                        this.setVisible(false);
                        new GameJFrame();
                    } else {
                        ShowJDialog("密码错误");
                    }
                } else {
                    ShowJDialog("用户名不存在");
                }

            } else {
                ShowJDialog("验证码错误");
                codeNum = codeUtils.getCode();
                code.setText(codeNum);
                this.getContentPane().repaint();
            }
        }
    } else if (oj == register) {
        register.setIcon(new ImageIcon("src\\Image\\Login\\微信图片_20241124120027.jpg"));
        this.getContentPane().repaint();
    }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }


    private boolean VerifyJudge(String str) {
//        for (int i = 0; i < str.length(); i++) {
//            char a=str.charAt(i);
//            char b=codeStr.charAt(i);
//            if(a!=b){
//                return false;
//            }
//        }
//        return true;
        if (codeNum.equalsIgnoreCase(str)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean UsernameJudge(String str) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUsername().equals(str)) {
                return true;
            }
        }
        return false;
    }

    private boolean PasswordJudge(String str) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getPassword().equals(str)) {
                return true;
            }
        }
        return false;
    }

    private void ShowJDialog(String context) {
        JDialog jDialog = new JDialog();

        jDialog.setSize(200, 150);

        jDialog.setAlwaysOnTop(true);

        jDialog.setLocationRelativeTo(null);

        jDialog.setModal(true);

        JLabel warning = new JLabel(context);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);

        jDialog.setVisible(true);
    }
}
